<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>GNUcode.me</title><id>https://gnucode.me/feeds/tags/Guix toolchains.xml</id><subtitle>Tag: Guix toolchains</subtitle><updated>2024-05-08T13:40:23Z</updated><link href="https://gnucode.me/feeds/tags/Guix toolchains.xml" rel="self" /><link href="https://gnucode.me" /><entry><title>Building Toolchains with Guix</title><id>https://gnucode.me/building-toolchains-with-guix.html</id><author><name>Mitchell Schmeisser &lt;mitchellschmeisser@librem.one&gt;</name><email>jbranso@dismail.de</email></author><updated>2023-02-23T23:00:00Z</updated><link href="https://gnucode.me/building-toolchains-with-guix.html" rel="alternate" /><content type="html">&lt;p&gt;Today's post is a guest post from my new internet friend Mitchell. We met on the
#guix irc channel, and I offered to post a few of his blog posts on this blog.  Without further ado,
here is Michell's first blog post (it's pretty fantastic)!&lt;/p&gt;&lt;h1&gt;Overview&lt;/h1&gt;&lt;p&gt;In order to deploy embedded software using Guix we first need to teach Guix
how to build it. Since Guix bootstraps everything this means we must teach Guix
how to build our toolchain.&lt;/p&gt;&lt;p&gt;The &lt;a href=&quot;https://zephyrproject.org&quot;&gt;Zephyr Project&lt;/a&gt; uses its own fork of GCC with custom configs for
the architectures supported by the project.&lt;/p&gt;&lt;h1&gt;Anatomy of a toolchain&lt;/h1&gt;&lt;p&gt;Toolchains are responsible for taking high level descriptions of programs and
lowering them down to a series of equivalent machine instructions. This process
involves more than just a compiler. The compiler uses the &lt;code&gt;binutils&lt;/code&gt; to
manipulate it’s internal representation down to a given architecture. It
also needs the C standard library as well as a few other libraries needed for
some compiler optimizations.&lt;/p&gt;&lt;p&gt;The C library provides the interface to the underlying kernel. System calls like &lt;code&gt;write&lt;/code&gt;
and &lt;code&gt;read&lt;/code&gt; are provided by &lt;code&gt;Glibc&lt;/code&gt; on most Linux distributions.&lt;/p&gt;&lt;p&gt;In embedded systems smaller implementations like &lt;code&gt;newlib&lt;/code&gt; and &lt;code&gt;newlib-nano&lt;/code&gt; are used.&lt;/p&gt;&lt;h1&gt;Bootstrapping a Toolchain&lt;/h1&gt;&lt;p&gt;In order to compile GCC we need a C library that’s been compiled for
our target architecture. How can we cross compile our C library if we
need our C library to build a cross compiler? The solution is to build
a simpler compiler that doesn’t require the C library to function.
It will not be capable of as many optimizations and it will be very slow,
however it will be able to build the C libraries as well as the complete version
of GCC.&lt;/p&gt;&lt;p&gt;In order to build the simpler compiler we need to compile the &lt;code&gt;binutils&lt;/code&gt; to
work with our target architecture.
The &lt;code&gt;binutils&lt;/code&gt; can be bootstrapped with our host GCC and have no target dependencies.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://crosstool-ng.github.io/docs/toolchain-construction/&quot;&gt;For more information read this.&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Doesn’t sound so bad right? It isn’t… in theory.
However internet forums since time immemorial have been
littered with the laments of those who came before.
From incorrect versions of &lt;code&gt;ISL&lt;/code&gt; to the wrong C library being linked
or the host linker being used, etc.
The one commonality between all of these issues is the environment.
Building GCC is difficult because isolating build environments is hard.&lt;/p&gt;&lt;p&gt;In fact as of &lt;code&gt;v0.14.2&lt;/code&gt; the zephyr SDK repository took down the build
instructions and posted a sign that read “Building this is too
complicated, don’t worry about it.” (I’m paraphrasing, but
&lt;a href=&quot;https://github.com/zephyrproject-rtos/sdk-ng/tree/v0.14.2#build-process&quot;&gt;not by
much&lt;/a&gt;.)&lt;/p&gt;&lt;p&gt;We will neatly side step all of these problems and not
risk destroying or polluting our host system with garbage
by using Guix to manage our environments for us.&lt;/p&gt;&lt;p&gt;Our toolchain only requires the first pass compiler because
newlib(-nano) is statically linked and introduced to the toolchain
by normal package composition.&lt;/p&gt;&lt;h1&gt;Defining the Packages&lt;/h1&gt;&lt;p&gt;All of the base packages are defined in &lt;code&gt;zephyr/packages/zephyr.scm&lt;/code&gt;.
Zephyr modules are defined in &lt;code&gt;zephyr/packages/zephyr-xyz.scm&lt;/code&gt;, following
the pattern of other module systems implemented by Guix.&lt;/p&gt;&lt;h2&gt;Binutils&lt;/h2&gt;&lt;p&gt;First thing we need to build is the &lt;code&gt;arm-zephyr-eabi&lt;/code&gt; binutils.
This is very easy in Guix.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(define-module (zephyr packages zephyr)
    #:use-module (guix packages)

(define-public arm-zephyr-eabi-binutils
  (let ((xbinutils (cross-binutils &amp;quot;arm-zephyr-eabi&amp;quot;)))
    (package
  (inherit xbinutils)
  (name &amp;quot;arm-zephyr-eabi-binutils&amp;quot;)
  (version &amp;quot;2.38&amp;quot;)
  (source
   (origin (method git-fetch)
       (uri (git-reference
             (url &amp;quot;https://github.com/zephyrproject-rtos/binutils-gdb&amp;quot;)
             (commit &amp;quot;6a1be1a6a571957fea8b130e4ca2dcc65e753469&amp;quot;)))
       (file-name (git-file-name name version))
       (sha256 (base32 &amp;quot;0ylnl48jj5jk3jrmvfx5zf8byvwg7g7my7jwwyqw3a95qcyh0isr&amp;quot;))))
  (arguments
   `(#:tests? #f
     ,@(substitute-keyword-arguments (package-arguments xbinutils)
         ((#:configure-flags flags)
      `(cons &amp;quot;--program-prefix=arm-zephyr-eabi-&amp;quot; ,flags)))))
  (native-inputs
   (append
    (list texinfo
      bison
      flex
      gmp
      dejagnu)
    (package-native-inputs xbinutils)))
  (home-page &amp;quot;https://zephyrproject.org&amp;quot;)
  (synopsis &amp;quot;binutils for zephyr RTOS&amp;quot;))))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The function &lt;code&gt;cross-binutils&lt;/code&gt; returns a package which has been
configured for the given gnu triplet.  We simply inherit that package
and replace the source.
The zephyr build system expects the binutils to be prefixed with
&lt;code&gt;arm-zephyr-eabi-&lt;/code&gt; which is accomplished by adding another flag to the
&lt;code&gt;#:configure-flags&lt;/code&gt; argument.&lt;/p&gt;&lt;p&gt;We can test our package definition using the &lt;code&gt;-L&lt;/code&gt; flag with &lt;code&gt;guix build&lt;/code&gt;
to add our packages.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;guix build -L guix-zephyr zephyr-binutils

/gnu/store/a947nb4rb2vymz2gaqnafgm1bsq4ipqp-zephyr-binutils-2.38&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This directory contains the results of &lt;code&gt;make install&lt;/code&gt;.&lt;/p&gt;&lt;h2&gt;GCC sans libc&lt;/h2&gt;&lt;p&gt;This one is a bit more involved. Don’t be afraid!
This version of GCC wants ISL version 0.15. It’s easy enough
to make that happen. Inherit the current version of ISL and swap
out the source and update the version. For most packages the build process doesn’t
change that much between versions.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(define-public isl-0.15
  (package
(inherit isl)
(version &amp;quot;0.15&amp;quot;)
(source (origin
      (method url-fetch)
      (uri (list (string-append &amp;quot;mirror://sourceforge/libisl/isl-&amp;quot;
                    version &amp;quot;.tar.gz&amp;quot;)))
      (sha256
       (base32
        &amp;quot;11vrpznpdh7w8jp4wm4i8zqhzq2h7nix71xfdddp8xnzhz26gyq2&amp;quot;))))))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Like the binutils, there is a function for creating cross-gcc packages. This one
accepts keywords specifying which binutils and libc to use. If libc isn’t
given (like here), gcc is configured with many options disabled to facilitate
being built without libc. Therefore we need to add the extra options we want (I
got them from the SDK configuration scripts on the &lt;a href=&quot;https://github.com/zephyrproject-rtos/sdk-ng&quot;&gt;sdk
github&lt;/a&gt; as well as the commits to
use for each of the tools. ).&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(define-public gcc-arm-zephyr-eabi-12
  (let ((xgcc (cross-gcc &amp;quot;arm-zephyr-eabi&amp;quot;
             #:xbinutils zephyr-binutils)))
    (package
  (inherit xgcc)
  (version &amp;quot;12.1.0&amp;quot;)
  (source (origin (method git-fetch)
          (uri (git-reference
                (url &amp;quot;https://github.com/zephyrproject-rtos/gcc&amp;quot;)
                (commit &amp;quot;0218469df050c33479a1d5be3e5239ac0eb351bf&amp;quot;)))
          (file-name (git-file-name (package-name xgcc) version))
          (sha256
           (base32 &amp;quot;1s409qmidlvzaw1ns6jaanigh3azcxisjplzwn7j2n3s33b76zjk&amp;quot;))
          (patches
           (search-patches &amp;quot;gcc-12-cross-environment-variables.patch&amp;quot;
                   &amp;quot;gcc-cross-gxx-include-dir.patch&amp;quot;))))
  (native-inputs
   (modify-inputs (package-native-inputs xgcc)
     ;; Get rid of stock ISL
     (delete &amp;quot;isl&amp;quot;)
     ;; Add additional dependencies that xgcc doesn't have
     ;; including our special ISL
     (prepend flex
          perl
          python-3
          gmp
          isl-0.15
          texinfo
          python
          mpc
          mpfr
          zlib)))
  (arguments
   (substitute-keyword-arguments (package-arguments xgcc)
     ((#:phases phases)
      `(modify-phases ,phases
         (add-after 'unpack 'fix-genmultilib
       (lambda _
         (substitute* &amp;quot;gcc/genmultilib&amp;quot;
           ((&amp;quot;#!/bin/sh&amp;quot;) (string-append &amp;quot;#!&amp;quot; (which &amp;quot;sh&amp;quot;))))
         #t))

         (add-after 'set-paths 'augment-CPLUS_INCLUDE_PATH
       (lambda* (#:key inputs #:allow-other-keys)
         (let ((gcc (assoc-ref inputs  &amp;quot;gcc&amp;quot;)))
           ;; Remove the default compiler from CPLUS_INCLUDE_PATH to
           ;; prevent header conflict with the GCC from native-inputs.
           (setenv &amp;quot;CPLUS_INCLUDE_PATH&amp;quot;
               (string-join
                (delete (string-append gcc &amp;quot;/include/c++&amp;quot;)
                    (string-split (getenv &amp;quot;CPLUS_INCLUDE_PATH&amp;quot;)
                          #\:))
                &amp;quot;:&amp;quot;))
           (format #t
               &amp;quot;environment variable `CPLUS_INCLUDE_PATH' changed to ~a~%&amp;quot;
               (getenv &amp;quot;CPLUS_INCLUDE_PATH&amp;quot;))
           #t)))))

     ((#:configure-flags flags)
      ;; The configure flags are largely identical to the flags used by the
      ;; &amp;quot;GCC ARM embedded&amp;quot; project.
      `(append (list &amp;quot;--enable-multilib&amp;quot;
             &amp;quot;--with-newlib&amp;quot;
             &amp;quot;--with-multilib-list=rmprofile&amp;quot;
             &amp;quot;--with-host-libstdcxx=-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm&amp;quot;
             &amp;quot;--enable-plugins&amp;quot;
             &amp;quot;--disable-decimal-float&amp;quot;
             &amp;quot;--disable-libffi&amp;quot;
             &amp;quot;--disable-libgomp&amp;quot;
             &amp;quot;--disable-libmudflap&amp;quot;
             &amp;quot;--disable-libquadmath&amp;quot;
             &amp;quot;--disable-libssp&amp;quot;
             &amp;quot;--disable-libstdcxx-pch&amp;quot;
             &amp;quot;--disable-nls&amp;quot;
             &amp;quot;--disable-shared&amp;quot;
             &amp;quot;--disable-threads&amp;quot;
             &amp;quot;--disable-tls&amp;quot;
             &amp;quot;--with-gnu-ld&amp;quot;
             &amp;quot;--with-gnu-as&amp;quot;
             &amp;quot;--enable-initfini-array&amp;quot;)
           (delete &amp;quot;--disable-multilib&amp;quot; ,flags)))))
  (native-search-paths
   (list (search-path-specification
      (variable &amp;quot;CROSS_C_INCLUDE_PATH&amp;quot;)
      (files '(&amp;quot;arm-zephyr-eabi/include&amp;quot;)))
         (search-path-specification
      (variable &amp;quot;CROSS_CPLUS_INCLUDE_PATH&amp;quot;)
      (files '(&amp;quot;arm-zephyr-eabi/include&amp;quot;
           &amp;quot;arm-zephyr-eabi/c++&amp;quot;
           &amp;quot;arm-zephyr-eabi/c++/arm-zephyr-eabi&amp;quot;)))
         (search-path-specification
      (variable &amp;quot;CROSS_LIBRARY_PATH&amp;quot;)
      (files '(&amp;quot;arm-zephyr-eabi/lib&amp;quot;)))))
  (home-page &amp;quot;https://zephyrproject.org&amp;quot;)
  (synopsis &amp;quot;GCC for zephyr RTOS&amp;quot;))))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This GCC can be built like so.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;guix build -L guix-zephyr gcc-cross-sans-libc-arm-zephyr-eabi

/gnu/store/qmp8bzmwwimw0r6fh165hgfhkxkxilpj-gcc-cross-sans-libc-arm-zephyr-eabi-12.1.0-lib
/gnu/store/38rli0rbn7ksmym3wq99cr4p2cjdz4a7-gcc-cross-sans-libc-arm-zephyr-eabi-12.1.0&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Great! We now have our stage-1 compiler.&lt;/p&gt;&lt;h2&gt;Newlib(-nano)&lt;/h2&gt;&lt;p&gt;The newlib package is quite straight forward (relatively).
It is mostly adding in the relevent configuration flags and patching
the files the &lt;code&gt;patch-shebangs&lt;/code&gt; phase missed.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;  (define-public zephyr-newlib
  (package
    (name &amp;quot;zephyr-newlib&amp;quot;)
    (version &amp;quot;3.3&amp;quot;)
    (source (origin
          (method git-fetch)
          (uri (git-reference
            (url &amp;quot;https://github.com/zephyrproject-rtos/newlib-cygwin&amp;quot;)
            (commit &amp;quot;4e150303bcc1e44f4d90f3489a4417433980d5ff&amp;quot;)))
          (sha256
           (base32 &amp;quot;08qwjpj5jhpc3p7a5mbl7n6z7rav5yqlydqanm6nny42qpa8kxij&amp;quot;))))
    (build-system gnu-build-system)
    (arguments
     `(#:out-of-source? #t
       #:configure-flags '(&amp;quot;--target=arm-zephyr-eabi&amp;quot;
               &amp;quot;--enable-newlib-io-long-long&amp;quot;
               &amp;quot;--enable-newlib-io-float&amp;quot;
               &amp;quot;--enable-newlib-io-c99-formats&amp;quot;
               &amp;quot;--enable-newlib-retargetable-locking&amp;quot;
               &amp;quot;--enable-newlib-lite-exit&amp;quot;
               &amp;quot;--enable-newlib-multithread&amp;quot;
               &amp;quot;--enable-newlib-register-fini&amp;quot;
               &amp;quot;--enable-newlib-extra-sections&amp;quot;
               &amp;quot;--disable-newlib-wide-orient&amp;quot;
               &amp;quot;--disable-newlib-fseek-optimization&amp;quot;
               &amp;quot;--disable-newlib-supplied-syscalls&amp;quot;
               &amp;quot;--disable-newlib-target-optspace&amp;quot;
               &amp;quot;--disable-nls&amp;quot;)
       #:phases
       (modify-phases %standard-phases
     (add-after 'unpack 'fix-references-to-/bin/sh
       (lambda _
         (substitute* '(&amp;quot;libgloss/arm/cpu-init/Makefile.in&amp;quot;
                &amp;quot;libgloss/arm/Makefile.in&amp;quot;
                &amp;quot;libgloss/libnosys/Makefile.in&amp;quot;
                &amp;quot;libgloss/Makefile.in&amp;quot;)
           ((&amp;quot;/bin/sh&amp;quot;) (which &amp;quot;sh&amp;quot;)))
         #t)))))
    (native-inputs
     `((&amp;quot;xbinutils&amp;quot; ,zephyr-binutils)
       (&amp;quot;xgcc&amp;quot; ,gcc-arm-zephyr-eabi-12)
       (&amp;quot;texinfo&amp;quot; ,texinfo)))
    (home-page &amp;quot;https://www.sourceware.org/newlib/&amp;quot;)
    (synopsis &amp;quot;C library for use on embedded systems&amp;quot;)
    (description &amp;quot;Newlib is a C library intended for use on embedded
systems.  It is a conglomeration of several library parts that are easily
usable on embedded products.&amp;quot;)
    (license (license:non-copyleft
          &amp;quot;https://www.sourceware.org/newlib/COPYING.NEWLIB&amp;quot;))))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And the build.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;guix build -L guix-zephyr zephyr-newlib

/gnu/store/4lx37gga1jv3ckykrxsfgwy9slaamln4-zephyr-newlib-3.3&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Complete toolchain&lt;/h2&gt;&lt;p&gt;Note that the toolchain is &lt;em&gt;Mostly&lt;/em&gt; complete. libstdc++ does not build because
`arm-zephyr-eabi` is not `arm-none-eabi` so a dynamic link check is
performed/failed. I cannot figure out how crosstool-ng handles this.&lt;/p&gt;&lt;p&gt;Anyway, now that we’ve got the individual tools it’s time to create
our complete toolchain. For this we need to do some package transformations.
Because these transformations are must be done for every combination of
binutils/gcc/newlib, it is best to create a function which we can reuse for
every version of the SDK.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(define (arm-zephyr-eabi-toolchain xgcc newlib version)
  &amp;quot;Produce a cross-compiler zephyr toolchain package with the compiler XGCC and the C
library variant NEWLIB.&amp;quot;
  (let ((newlib-with-xgcc (package (inherit newlib)
                   (native-inputs
                    (alist-replace &amp;quot;xgcc&amp;quot; (list xgcc)
                           (package-native-inputs newlib))))))
    (package
  (name (string-append &amp;quot;arm-zephyr-eabi&amp;quot;
               (if (string=? (package-name newlib-with-xgcc)
                     &amp;quot;newlib-nano&amp;quot;)
               &amp;quot;-nano&amp;quot; &amp;quot;&amp;quot;)
               &amp;quot;-toolchain&amp;quot;))
  (version version)
  (source #f)
  (build-system trivial-build-system)
  (arguments
   '(#:modules ((guix build union)
            (guix build utils))
     #:builder
     (begin
       (use-modules (ice-9 match)
            (guix build union)
            (guix build utils))
       (let ((out (assoc-ref %outputs &amp;quot;out&amp;quot;)))
         (mkdir-p out)
         (match %build-inputs
       (((names . directories) ...)
        (union-build (string-append out &amp;quot;/arm-zephyr-eabi&amp;quot;)
                 directories)
        #t))))))
  (inputs
   `((&amp;quot;binutils&amp;quot; ,zephyr-binutils)
     (&amp;quot;gcc&amp;quot; ,xgcc)
     (&amp;quot;newlib&amp;quot; ,newlib-with-xgcc)))
  (synopsis &amp;quot;Complete GCC tool chain for ARM zephyrRTOS development&amp;quot;)
  (description &amp;quot;This package provides a complete GCC tool chain for ARM
bare metal development with zephyr rtos.  This includes the GCC arm-zephyr-eabi cross compiler
and newlib (or newlib-nano) as the C library.  The supported programming
language is C.&amp;quot;)
  (home-page (package-home-page xgcc))
  (license (package-license xgcc)))))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This function creates a special package which consists of the toolchain in a special directory hierarchy, i.e &lt;code&gt;arm-zephyr-eabi/&lt;/code&gt;.
Our complete toolchain definition looks like this.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(define-public arm-zephyr-eabi-toolchain-0.15.0
  (arm-zephyr-eabi-toolchain
   gcc-arm-zephyr-eabi-12
   zephyr-newlib
   &amp;quot;0.15.0&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To build:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;guix build -L guix-zephyr arm-zephyr-eabi-toolchain

/gnu/store/9jnanr27v6na5qq3dlgljraysn8r1sad-arm-zephyr-eabi-toolchain-0.15.0&lt;/code&gt;&lt;/pre&gt;&lt;h1&gt;Integrating with Zephyr Build System&lt;/h1&gt;&lt;p&gt;Zephyr uses CMake as it’s build system. It contains numerous CMake files in both the so-called &lt;code&gt;ZEPHYR_BASE&lt;/code&gt;,
the zephyr source code repository, as well as a handful in the SDK which help select the correct toolchain
for a given board.&lt;/p&gt;&lt;p&gt;There are standard locations the build system will look for the SDK. We are not
using any of them. Our SDK lives in the store, immutable forever. According to
&lt;a href=&quot;https://docs.zephyrproject.org/latest/develop/west/without-west.html&quot;&gt;this
webpage&lt;/a&gt;,
the variable &lt;code&gt;ZEPHYR_SDK_INSTALL_DIR&lt;/code&gt; needs to point to our custom spot.&lt;/p&gt;&lt;p&gt;We also need to grab the cmake files from the &lt;a href=&quot;https://github.com/zephyrproject-rtos/sdk-ng&quot;&gt;repository&lt;/a&gt; and create a file &lt;code&gt;sdk_version&lt;/code&gt; which
contains the version string &lt;code&gt;ZEPHYR_BASE&lt;/code&gt; uses to find a compatible SDK.&lt;/p&gt;&lt;p&gt;Along with the SDK proper we need to include a number of python packages required by the build system.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;  (define-public zephyr-sdk
  (package
    (name &amp;quot;zephyr-sdk&amp;quot;)
    (version &amp;quot;0.15.0&amp;quot;)
    (home-page &amp;quot;https://zephyrproject.org&amp;quot;)
    (source (origin (method git-fetch)
            (uri (git-reference
              (url &amp;quot;https://github.com/zephyrproject-rtos/sdk-ng&amp;quot;)
              (commit &amp;quot;v0.15.0&amp;quot;)))
            (file-name (git-file-name name version))
            (sha256 (base32 &amp;quot;04gsvh20y820dkv5lrwppbj7w3wdqvd8hcanm8hl4wi907lwlmwi&amp;quot;))))
    (build-system trivial-build-system)
    (arguments
     `(#:modules ((guix build union)
          (guix build utils))
       #:builder
       (begin
     (use-modules (guix build union)
              (ice-9 match)
              (guix build utils))
     (let* ((out (assoc-ref %outputs &amp;quot;out&amp;quot;))
        (cmake-scripts (string-append (assoc-ref %build-inputs &amp;quot;source&amp;quot;)
                          &amp;quot;/cmake&amp;quot;))
        (sdk-out (string-append out &amp;quot;/zephyr-sdk-0.15.0&amp;quot;)))
       (mkdir-p out)

       (match (assoc-remove! %build-inputs &amp;quot;source&amp;quot;)
         (((names . directories) ...)
          (union-build sdk-out directories)))

       (copy-recursively cmake-scripts
                 (string-append sdk-out &amp;quot;/cmake&amp;quot;))

       (with-directory-excursion sdk-out
         (call-with-output-file &amp;quot;sdk_version&amp;quot;
           (lambda (p)
         (format p &amp;quot;0.15.0&amp;quot;)))
         #t)))))
    (propagated-inputs
     (list
      arm-zephyr-eabi-toolchain-0.15.0
      zephyr-binutils
      dtc))
    (native-search-paths
     (list (search-path-specification
        (variable &amp;quot;ZEPHYR_SDK_INSTALL_DIR&amp;quot;)
        (files '(&amp;quot;&amp;quot;)))))
    (synopsis &amp;quot;SDK for zephyrRTOS&amp;quot;)
    (description &amp;quot;zephyr-sdk contains bundles a complete gcc toolchain as well
as host tools like dtc, openocd, qemu, and required python packages.&amp;quot;)
    (license license:apsl2)))&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Testing&lt;/h2&gt;&lt;p&gt;In order to test we will need an environment with the SDK installed.
We can take advantage of &lt;code&gt;guix shell&lt;/code&gt; to avoid installing test packages into
our home environment. This way, if it causes problems, we can just exit the shell
and try again.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;guix shell -L guix-zephyr zephyr-sdk cmake ninja git&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;ZEPHYR_BASE&lt;/code&gt; can be cloned into a temporary workspace to test our toolchain
functionality (For now. Eventually we will need to create a package for
&lt;code&gt;zephyr-base&lt;/code&gt; that our guix zephyr-build-system can use).&lt;/p&gt;&lt;pre&gt;&lt;code&gt;mkdir /tmp/zephyr-project
cd /tmp/zephyr-project
git clone https://github.com/zephyrproject-rtos/zephyr
export ZEPHYR_BASE=/tmp/zephyr-project/zephyr&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In order to build for the test board (k64f in this case) we need to get a hold
of the vendor Hardware Abstraction Layers and CMSIS (These will also need to
become guix packages to allow the build system to compose modules).&lt;/p&gt;&lt;pre&gt;&lt;code&gt;git clone https://github.com/zephyrproject-rtos/hal_nxp &amp;amp;&amp;amp;
git clone https://github.com/zephyrproject-rtos/cmsis&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To inform the build system about this module we pass it in with &lt;code&gt;-DZEPHYR_MODULES=&lt;/code&gt; which is
a semicolon separated list of paths containing a module.yml file.&lt;/p&gt;&lt;p&gt;To build the hello world sample we use the following incantation.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;cmake -Bbuild $ZEPHYR_BASE/samples/hello_world \
  -GNinja \
  -DBOARD=frdm_k64f \
  -DBUILD_VERSION=3.1.0 \
  -DZEPHYR_MODULES=&amp;quot;/tmp/zephyr-project/hal_nxp;/tmp/zephyr-project/cmsis&amp;quot; \
    &amp;amp;&amp;amp; ninja -Cbuild&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If everything is set up correctly we will end up with a &lt;code&gt;./build&lt;/code&gt;
directory with all our build artifacts. The SDK is installed correctly!&lt;/p&gt;</content></entry></feed>